#############################################################################
#############################################################################
#Divisive or Descriptive?: How Americans Understand Critical Race Theory
#Safarpour, Lunz Trujillo, Green, Pippert, Lin, and Druckman.
#Journal of Race, Ethnicity, and Politics (2024)
#Code for Figure 1 (E-News Data).
#############################################################################
#############################################################################

rm(list = ls())

library(tidyverse)
library(lubridate)

crt_df <- readr::read_csv("dcinbox_export__critical r.csv",
                          col_types = c("cccciccDccicccc"))

names(crt_df) <- make.names(names(crt_df))

crt_df$Unix.Timestamp <- substr(crt_df$Unix.Timestamp, start = 1, stop = nchar(crt_df$Unix.Timestamp)-3)

crt_df$date <- lubridate::as_date(as_datetime(as.numeric(crt_df$Unix.Timestamp)))


crt_mentions_enews <- 
  crt_df %>%
  group_by(Party, date) %>%
  summarise(n = n()) %>%
  arrange(date) %>%
  group_by(Party) %>%
  mutate(cumulative_mentions = cumsum(n)) %>%
  ggplot(aes(x = date, y = cumulative_mentions, col = Party))+
  geom_line()+
  scale_color_manual(name = "Party",
                     breaks = c("Democrat","Republican"),
                     values = c("blue","red"),
                     labels = c("Democrat\n(3 total)","Republican\n(453 total)"))+
  labs(x = "Date (Binned by Week)",
       y = "Cumulative Mentions",
       title = "References to 'critical race theory' in congressional e-newsletters",
       subtitle = "April 20, 2021 - September 5, 2023",
       caption = "Data from dcinbox.com")+
  theme_classic()


ggsave(crt_mentions_enews, file = "SafarpouretalFig1.pdf", width = 8, height = 4)
